/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "$/attractions/inspection/api/inspectionException.ts";
import { page as getDevicesPage } from "$/attractions/inspection/api/inspectionDevices.ts";
import useDrawer from "@/hooks/useDrawer";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any,
  drawer?: any
): MaProTableColumns[] {

  const fullUrl = window.location;
  console.log(fullUrl);
  const msg = useMessage();

  const deviceList = ref<any[]>([]);
  
  onMounted(async () => {
    // 获取设备列表
    const deviceResponse = await getDevicesPage({});
    if (
      deviceResponse.code === 200 &&
      deviceResponse.data &&
      deviceResponse.data.list
    ) {
      deviceList.value = deviceResponse.data.list;
    }
  });

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "事件类型",
      prop: "type",
      cellRender: ({ row }) => {
        const typeMap = {
          1: { text: "故障", type: "danger" },
          2: { text: "告警", type: "warning" },
        };
        const type = typeMap[row.type] || {
          text: row.type || "未知",
          type: "info",
        };
        return h(ElTag, { type: type.type }, () => type.text);
      },
    },
    { label: "异常时间", prop: "date", mode: "date", showTime: true },
    { label: "事件内容/描述", prop: "content" },
    {
      label: "处理状态",
      prop: "status",
      cellRender: ({ row }) => {
        const statusMap = {
          1: { text: "已处理", type: "success" },
          2: { text: "未处理", type: "danger" },
          3: { text: "已接收", type: "warning" },
        };
        const status = statusMap[row.status] || { text: "未知", type: "info" };
        return h(ElTag, { type: status.type }, () => status.text);
      },
    },
    {
      label: "巡检设备",
      prop: "devices",
      cellRender: ({ row }) => {
        if (!row.devices || !Array.isArray(row.devices)) {
          return "无设备";
        }
        const deviceNames = row.devices.map((deviceId: number) => {
          const device = deviceList.value.find((item) => item.id === deviceId);
          return device ? device.name : `设备${deviceId}`;
        });
        return deviceNames.join(", ");
      },
    },
    { label: "巡检开始时间", prop: "start_time", mode: "date", showTime: true },
    { label: "巡检结束时间", prop: "end_time", mode: "date", showTime: true },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "detail",
            icon: "ep:view",
            text: () => "详情",
            onClick: ({ row }) => {
              if (drawer) {
                drawer.setTitle("临时巡检详情");
                drawer.open({ 
                  width: 800,
                  data: { inspectionRecordId: row.id }
                });
              }
            },
          },
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) =>
              hasAuth("inspection_exception:inspectionException:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) =>
              hasAuth("inspection_exception:inspectionException:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
